﻿statusloginform = 0;
ipuser = null;
data =  null ;

function controls(){	
	statusViewProducts = 0;
	statusHide = 0;
	url = 'http://localhost:8888/shopping365/';
	
	this.cuahang = function(id){
		$("document").ready(function(){
			$(this).click(function(){
				$("#xemthem").removeClass("active");
				$("#sanpham").removeClass("active");
				$("#bando").removeClass("active");
				$("#comment").removeClass("active");
				$("#lienhe").removeClass("active");			
				$("#"+id).addClass("active");
			});
		});
	}
	
	this.convert = function(style,text){
		if(style==='chuyenmuc'){
			if(text==='tt') return "THỜI TRANG";
		} else if(style==='loaisp'){
			if(strtolower(text)==="aonu") return "ÁO NỮ ";
		}
	}
	
	this.tooltip = function(id){
		$("document").ready(function(){
			$(this).click(function(){
				$("#"+id).addClass("tooltip");			
			});
		});
	}
	
	this.zoom = function(id){
		$(document).ready(function(){
			$('#'+id).zoom();
		});
	}

	this.unhide = function(id){
		$("document").ready(function(){
			$(this).click(function(){
				$("#"+id).addClass("display subchuyenmuc");			
			});
		});
	}
	
	this.ref = function(id,namepage){
		if(namepage=='shop'){	
			sessionStorage.cuahang = id ;
			window.location.href=(url+"cuahang.php?id="+id);
		}else if(namepage=='sanpham'){
			$loaishop = id.split('_');
			window.location.href=(url+"sanpham.php?cm="+$loaishop[0]+"&id="+$loaishop[1]);
		}
	}
			
	this.setDataProduct = function(shopid,pid,namepage){	
		sessionStorage.sanpham = "id="+shopid+"&pid="+pid ;
		window.location.href=(url+"model/getdata.php?id="+shopid+"&pid="+pid+"&page="+namepage);
	}
	
	this.reference = function(id){
		switch(id){
			case 1: {window.sessionStorage.chuyenmuc = "tt" ;location.href="chuyenmuc.php";break;}
			case 2: {window.sessionStorage.chuyenmuc = "pk" ;location.href="phukien.php";break;}
			case 3: {window.sessionStorage.chuyenmuc = "ld" ;location.href="lamdep.php";break;}
			case 4: {window.sessionStorage.chuyenmuc = "mvb" ;location.href="mevabe.php";break;}
			case 5: {window.sessionStorage.chuyenmuc = "ddt" ;location.href="dodientu.php";break;}
			case 6: {window.sessionStorage.chuyenmuc = "dgd" ;location.href="dogiadung.php";break;}
			case 7: {window.sessionStorage.chuyenmuc = "bds" ;location.href="batdongsan.php";break;}
			case 8: {window.sessionStorage.chuyenmuc = "tt" ;location.href="dichvu.php";break;}
			default : {window.sessionStorage.chuyenmuc = null ;location.href="khongtimthay.php";break;}
		}
	}
	
	this.day = function(){
		var d=new Date();
		var s=d.getSeconds();
		var m=d.getMinutes();
		var h=d.getHours();
		var day=d.getDay();
		var date=d.getDate();
		var month=d.getMonth();
		var year=d.getFullYear();
		var days=new Array("Chủ nhật","Thứ hai","Thứ 3","Thứ 4","Thứ 5","Thứ 6","Thứ 7");
		var months=new Array("1","2","3","4","5","6","7","8","9","10","11","12"); 
		var am_pm;
		if (s<10) {s="0" + s}
		if (m<10) {m="0" + m}
		if (h>12){h-=12;AM_PM = "PM"}
		else {AM_PM="AM"}
		if (h<10) {h="0" + h}
		document.getElementById("day").innerHTML = "THỨ : "+days[day]+" NGÀY : "+date+" THÁNG : "+months[month]+" NĂM : "+year ;
	}
	
	this.getData = function(pagenum,maxRecord,dataname){
		indexEnd = pagenum*maxRecord;
		var xmlhttp;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
		}			
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				document.getElementById("ss-container").innerHTML = xmlhttp.responseText ;
			}
		}
		xmlhttp.open("GET","../model/getdata.php?indexEndGetData="+indexEnd+"&maxRecord="+maxRecord+"&dname="+dataname,false);
		xmlhttp.send();	
	}
	
	this.chuyenmuc = function(tbl){
		$("#slideshowthoitrang").hide();
		var xmlhttp;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
		}			
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				document.getElementById("chuyenmuc").innerHTML = xmlhttp.responseText ;
			}
		}
		xmlhttp.open("GET","control/chuyenmuc.php?tbl="+tbl,false);
		xmlhttp.send();	
	}
	
	this.clock = function(){
		var timer = new Date(); 
		var hour = timer.getHours(); 
		var minute = timer.getMinutes(); 
		var second = timer.getSeconds(); 
		if(hour < 10) {
			hour = "0" + hour;
		}
		if(minute < 10) {
			minute = "0" + minute;
		}
		if(second < 10) {
			second = "0" + second;
		}
		document.getElementById("clock").innerHTML = hour + ":" + minute + ":" + second ;
	}
	
	this.delay = function(dl){
		var start = new Date().getTime();
		while (new Date().getTime() < start + dl);
	}
	
	this.viewpage = function(){		
		$("#info").hide();
		$(".sp-container").hide();	

		var xmlhttp;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
		}			
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                $("#index").html(xmlhttp.responseText);
			}
		}
		xmlhttp.open("GET","control/innerhome.php",true);
		xmlhttp.send();	
		/*window.location.reload();*/
	}
	
	this.area = function(id){
		if(id=='hanoi'){
			$("document").ready(function(){
				$(this).click(function(){
					$(".hanoi").removeClass("nonedisplay");
					$(".saigon").removeClass("display");
					$(".tinhkhac").removeClass("display");
					$(".saigon").addClass("nonedisplay");
					$(".tinhkhac").addClass("nonedisplay");
					$(".hanoi").addClass("display");
				});
			});
		}else if(id=='saigon'){
			$("document").ready(function(){
				$(this).click(function(){
					$(".saigon").removeClass("nonedisplay");
					$(".hanoi").removeClass("display");
					$(".tinhkhac").removeClass("display");
					$(".hanoi").addClass("nonedisplay");
					$(".tinhkhac").addClass("nonedisplay");
					$(".saigon").addClass("display");
				});
			});
		}else if(id=='tinhkhac'){
			$("document").ready(function(){
				$(this).click(function(){
					$(".tinhkhac").removeClass("nonedisplay");
					$(".hanoi").removeClass("display");
					$(".saigon").removeClass("display");
					$(".hanoi").addClass("nonedisplay");
					$(".saigon").addClass("nonedisplay");
					$(".tinhkhac").addClass("display");
				});
			});
		}else {
			$("document").ready(function(){
				$(this).click(function(){
					$(".tinhkhac").removeClass("nonedisplay");
					$(".saigon").removeClass("nonedisplay");
					$(".hanoi").removeClass("nonedisplay");
					$(".hanoi").addClass("display");
					$(".saigon").addClass("display");
					$(".tinhkhac").addClass("display");
				});
			});
		}
	}
	
	this.view = function(id){	
		if(statusloginform==0){
			$("document").ready(function(){
				$(this).click(function(){
					$("#loginform").removeClass("display");
					$("#registerform").removeClass("display");
					$("#"+id).addClass("display");
				});
			});
			++statusloginform;
		}else{
			$("document").ready(function(){
				$(this).click(function(){
					$("#"+id).removeClass("display");
				});
			});
			statusloginform = 0;
		}
	}
	
	this.hide = function(id){
		$("document").ready(function(){
			if(statusHide == 0){
				$("#"+id).removeClass("nonedisplay");
				$("#"+id).addClass("display");
			}else{
				$("#"+id).removeClass("display");
				$("#"+id).addClass("nonedisplay");
			}
			++statusHide;
			if(statusHide>1) statusHide = 0;
		});
	}
	
	this.show = function(loaisanpham,tenchuyenmuc){
		$("#thoitrang").hide();	
		var xmlhttp;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
		}			
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				document.getElementById("sanpham").innerHTML = xmlhttp.responseText ;
			}
		}
		xmlhttp.open("GET","/shopping365/control/innerchuyenmuc.php?view="+loaisanpham+"&tenchuyenmuc="+tenchuyenmuc,false);
		xmlhttp.send();
	}

	this.showproducts = function(divId,shopid){
		$("document").ready(function(){
			if(statusViewProducts == 0){
				$("#"+divId).removeClass("nonedisplay");
				$("#"+divId).addClass("display");
				var xmlhttp;
				if(window.XMLHttpRequest){
					xmlhttp = new XMLHttpRequest();
				}else{
					xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
				}			
				xmlhttp.onreadystatechange = function(){
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
						document.getElementById(divId).innerHTML = xmlhttp.responseText ;
					}
				}
				xmlhttp.open("GET","control/showproducts.php?shopid="+shopid,false);
				xmlhttp.send();
			}else{
				$("#"+divId).removeClass("display");
				$("#"+divId).addClass("nonedisplay");
			}
			++statusViewProducts;
			if(statusViewProducts>1) statusViewProducts = 0;
		});	
	}
	
	this.getTenChuyenMuc = function(nhomsp){
		if(nhomsp==="ttao" || nhomsp==="ttquan" || nhomsp ==="ttvay" || nhomsp==="ttkhantat" || nhomsp==="ttdolot" || nhomsp==="ttspkhac" ) return "Thời Trang";
		else if(nhomsp==="pkgiaydep" || nhomsp==="pkmunon" || nhomsp ==="pkdongho" || nhomsp==="pkmatkinh" || nhomsp==="pktrangsuc" || nhomsp==="pkspkhac" ) return "Phụ Kiện";
		else if(nhomsp==="ldthammyvien" || nhomsp==="ldspa" || nhomsp ==="ldthammyvien" || nhomsp==="ldtatooart" || nhomsp==="ldaocuoi" || nhomsp==="ldspkhac" ) return "Làm Đẹp";	
	}
	
	this.getTenNhomSP = function(nhomsp){
		if(nhomsp==="ttao" ) return "Áo";
		else if(nhomsp==="ttquan" ) return "Quần";
		else if(nhomsp==="ttvay" ) return "Váy";
		else if(nhomsp==="ttkhantat" ) return "Khăn & Tất";
		else if(nhomsp==="ttdolot" ) return "Đồ Lót";
		else if(nhomsp==="ttspkhac" ) return "Khác";
	}

	this.checklogin = function(){
		alert("test1");
		re=/^ *$/;
		re2=/^[A-Za-z0-9_]+$/;
		with(formlogin){
			alert("test2");
			if(re2.test(username.value)==false||re.test(username.value)||username.value.length<6||username.value.length>20){
				username.value="";
				username.focus();
				return false;
			}else if(re.test(password.value)||re2.test(password.value)==false||password.value.length<8||password.value.length>20){
				password.value="";
				password.focus();
				return false;
			}else{
				alert("test3");
				var xmlhttp;
				var str = username.value+">"+password.value+">a";
				if(window.XMLHttpRequest){
					xmlhttp = new XMLHttpRequest();
				}else{
					xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
				}
				xmlhttp.onreadystatechange = function(){
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
						alert("test5");
						var kq = xmlhttp.responseText ;
						if(kq=="true"){
							alert("Đăng nhập thành công !");
							username.value="";
							password.value="";
							$("document").ready(function(){
								$("#loginform").removeClass("display");
								statusloginform=0;
							});
							alert("test6");
							location.href="taikhoancanhan";
						} else {
							alert("Tên tài khoản hoặc mật khẩu không đúng !");
						}
					}
				}
				xmlhttp.open("GET","control/processlogin.php?login="+str,false);
				xmlhttp.send();
			}
		}
	}
	
	this.price = function(p){
		var temp = '';
		for(var i=p.length-1;i>=0;i--,j++){
			if( (i%3==0) && (i!=0) && (i!=p.length-1) ){ 
				temp += "," ;
			}
			temp += p[i] ;
		}
		return StrReverse(temp);
	}
	
	this.checksubmit = function(typesubmit){
		if(typesubmit==='registeraccount'){
			this.checkRegister();
		}
	}

	this.checkusername = function(){
		re=/^ *$/;
		re2=/^[A-Za-z0-9_]+$/;
		with(formregister){
			if(re2.test(username.value)==false||re.test(username.value)||username.value.length<6||username.value.length>20){
				alert("Tên đăng ký dài 6 đến 20 kí tự . Ví dụ : nguyenvanan2014 .");
				username.value="";
				username.focus();	
				return false;
			}else {
				var xmlhttp;
					if(window.XMLHttpRequest){
						xmlhttp = new XMLHttpRequest();
					}else{
						xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
					}			
					xmlhttp.onreadystatechange = function(){
						if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
							var kq = xmlhttp.responseText.trim();
							if(kq=="true"){
								alert("Tên đăng ký đã có người sử dụng ");
								username.value="";
								username.focus();	
								return false;
							}
						}
					}
					xmlhttp.open("GET","control/checkusername.php?checkname="+username.value,false);
					xmlhttp.send();
			}
		}
	}
	
	this.googlemap = function(address,shopname){
		var xmlhttp;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Miscrosoft.XMLHTTP");
		}			
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				document.getElementById("showmap").innerHTML = xmlhttp.responseText ;
			}
		}
		xmlhttp.open("GET","control/getmap.php?a='"+address+"'&s='"+shopname+"'",false);
		xmlhttp.send();
	}

}

